/*
 * @(#)HouseProvidentBean.java       2011-8-8
 *
 * Copyright（c）2010 北京云狐软件有限公司保留所有权利.
 */
package com.yunhoo.provident.bean;

import com.yunhoo.base.model.Person;
import com.yunhoo.provident.db.impl.HouseProvidentSqlImpl;
import com.yunhoo.provident.model.HouseProvident;
import com.yunhoo.provident.model.PersonProvident;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.CommonParam;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

/**
 * 类
 * <code>HouseProvidentBean</code>
 *
 * @author
 * @version 2011-8-8
 * @see java.lang.Class
 * @since jdk1.6
 */
@ManagedBean(name = "provident")
@SessionScoped
public class HouseProvidentBean {

    private String errmessage;
    private int uploadsAvailable = 5;
    private boolean autoUpload = false;
    private boolean useFlash = false;
    private PersonProvident per = new PersonProvident();
    private Person p = null;
    private HouseProvident hou = new HouseProvident();
    private List<HouseProvident> houList = null;
    private Integer iyear = null;
    private List<Integer> yearList = null;

    public HouseProvidentBean() {
        initYearList();
        p = LoginParam.getPer();
        System.out.println("cpsn_name : " + p.getCpsn_name());
        System.out.println("gjjzh:" + p.getGjjzh());
        if (!Null.check(p.getGjjzh())) {
            Map map = new HashMap();
            map.put("gjjzh", p.getGjjzh());
            map.put("orgCode", LoginParam.getOrg().getCode());
            per = (PersonProvident) IbatisTemplate.query("houseProvident.queryProvidentBook", map);
            //houList = IbatisTemplate.queryForList("houseProvident.queryHouseProvident", map);
        }
        iyear = CommonParam.defaultYear();
        ListHouseProvident();
    }

    private void initYearList() {
        yearList = CommonParam.yearListOfInteger();
    }

    public void queryHouseProvident() {
        ListHouseProvident();
    }
    
    private void ListHouseProvident(){        
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("iyear", iyear);
        map.put("gjjzh", p.getGjjzh());
        houList = IbatisTemplate.queryForList("houseProvident.queryHouseProvident", map);
    }

    public void listener(FileUploadEvent event) throws Exception {
        this.setErrmessage("");
        List<HouseProvident> lists = new ArrayList<HouseProvident>();
        try {
            UploadedFile item = event.getUploadedFile();
            InputStream myxls = new BufferedInputStream(new ByteArrayInputStream(item.getData()));
            HSSFWorkbook wb = new HSSFWorkbook(myxls);
            HSSFSheet sheet1 = wb.getSheetAt(0);
            System.out.println("rows:" + sheet1.getLastRowNum());

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            for (Row row : sheet1) {
                HouseProvident gjjb = new HouseProvident();
                Cell cellb = row.getCell(1);
                gjjb.setBtype(cellb.getStringCellValue());
                Cell cellc = row.getCell(2);
                Date bDate = format.parse(cellc.getStringCellValue());
                gjjb.setBdate(bDate);//
                Cell celld = row.getCell(3);
                gjjb.setAccount(celld.getStringCellValue());
                Cell celle = row.getCell(4);
                gjjb.setCpsn_name(celle.getStringCellValue());
                Cell cellf = row.getCell(5);
                gjjb.setSfzh(cellf.getStringCellValue());
                Cell cellg = row.getCell(6);
                Double g = cellg.getNumericCellValue();
                gjjb.setDebitAmount(g);
                Cell cellh = row.getCell(7);
                Double h = cellh.getNumericCellValue();
                gjjb.setCreditAmount(h);
                Cell celli = row.getCell(8);
                gjjb.setBankBranch(celli.getStringCellValue());
                Cell cellj = row.getCell(9);
                gjjb.setAssistant(cellj.getStringCellValue());
                lists.add(gjjb);
            }
            myxls.close();
            //out.close();
            HouseProvidentSqlImpl.delProvidentBookForImpNewData();
            HouseProvidentSqlImpl.insertProvident(lists);
            System.out.println("OK:" + lists.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void personListener(FileUploadEvent event) throws Exception {
        this.setErrmessage("");
        List<PersonProvident> list = new ArrayList<PersonProvident>();
        try {
            UploadedFile item = event.getUploadedFile();
            //InputStream myxls = new BufferedInputStream(new FileInputStream(file));
            InputStream myxlsp = new BufferedInputStream(new ByteArrayInputStream(item.getData()));
            HSSFWorkbook wb = new HSSFWorkbook(myxlsp);
            HSSFSheet sheet2 = wb.getSheetAt(0);
            System.out.println("rows:" + sheet2.getLastRowNum());

            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            for (int i = 1; i < sheet2.getLastRowNum(); i++) {
                Row row = sheet2.getRow(i);
                PersonProvident gjjp = new PersonProvident();
                Cell cellb = row.getCell(1);
                gjjp.setGjjaccount(Null.convert(cellb.getStringCellValue()));
                Cell cellc = row.getCell(2);
                gjjp.setCpsn_name(Null.convert(cellc.getStringCellValue()));
                //Date bDate = format.parse(cellc.getStringCellValue());
                //gjjb.setBdate(bDate);//
                Cell celld = row.getCell(3);
                gjjp.setSfzh(Null.convert(celld.getStringCellValue()));
                Cell celle = row.getCell(4);
                gjjp.setEmpclass(Null.convert(celle.getStringCellValue()));
                Cell cellf = row.getCell(5);
                gjjp.setEmpstatus(Null.convert(celle.getStringCellValue()));
                Cell cellg = row.getCell(6);
                gjjp.setInfoClear(cellg.getStringCellValue());
                Cell cellh = row.getCell(7);
                gjjp.setDepartment(Null.convert(cellh.getStringCellValue()));
                Cell celli = row.getCell(8);
                Double ri = celli.getNumericCellValue();
                gjjp.setDepoBase(Null.convert(ri));
                Cell cellj = row.getCell(9);
                Double j = cellj.getNumericCellValue();
                gjjp.setMonthAmount(Null.convert(j));
                Cell cellk = row.getCell(10);
                Double k = cellk.getNumericCellValue();
                gjjp.setYearAmount(Null.convert(k));
                Cell cellm = row.getCell(11);
                Double m = cellm.getNumericCellValue();
                gjjp.setYearAdded(Null.convert(m));
                Cell celln = row.getCell(12);
                Double n = celln.getNumericCellValue();
                gjjp.setYearWithdraw(Null.convert(n));
                Cell cello = row.getCell(13);
                Double o = cello.getNumericCellValue();
                gjjp.setAccDepoAmount(Null.convert(o));
                Cell cellp = row.getCell(14);
                Double p = cellp.getNumericCellValue();
                gjjp.setAccWithDrawAmount(Null.convert(p));
                Cell cellr = row.createCell(15);
                Double r = cellr.getNumericCellValue();
                gjjp.setCurBalance(Null.convert(r));
                Cell cells = row.getCell(16);
                Double s = cells.getNumericCellValue();
                gjjp.setRegularBalance(Null.convert(s));
                Cell cellt = row.getCell(17);
                gjjp.setEntrustWithdrawSta(Null.convert(cellt.getStringCellValue()));
                Cell cellu = row.getCell(18);
                Date uDate = format.parse(cellu.getStringCellValue());
                gjjp.setAccountDate(Null.convert(uDate));
                Cell cellv = row.getCell(19);
                gjjp.setCancelReason(Null.convert(cellv.getStringCellValue()));
                Cell cellw = row.getCell(20);
                gjjp.setCancelDate(stringToDate(cellw.getStringCellValue()));
                Cell cellx = row.getCell(21);
                gjjp.setArchReason(Null.convert(cellx.getStringCellValue()));
                Cell celly = row.getCell(22);
//                if(gjjp.getArchDate() == null){
//                    celly.setCellValue("");
//                }else{Date yDate = format.parse(celly.getStringCellValue());
//                gjjp.setArchDate(Null.convert(yDate));
//                }
                gjjp.setArchDate(stringToDate(celly.getStringCellValue()));
                Cell cellz = row.getCell(23);
                if (gjjp.getTuneOutDate() == null) {
                    cellz.setCellValue("");
                } else {
                    Date zDate = format.parse(cellz.getStringCellValue());
                    gjjp.setTuneOutDate(Null.convert(zDate));
                }

                list.add(gjjp);
            }

            myxlsp.close();
            //out.close();
            HouseProvidentSqlImpl.insertProvidentBook(list);
            System.out.println("OK:" + list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Date stringToDate(String str) {
        Date date = null;
        if (str == null || str.length() == 0) {
            return null;
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public boolean isAutoUpload() {
        return autoUpload;
    }

    public void setAutoUpload(boolean autoUpload) {
        this.autoUpload = autoUpload;
    }

    public String getErrmessage() {
        return errmessage;
    }

    public void setErrmessage(String errmessage) {
        this.errmessage = errmessage;
    }

    public int getUploadsAvailable() {
        return uploadsAvailable;
    }

    public void setUploadsAvailable(int uploadsAvailable) {
        this.uploadsAvailable = uploadsAvailable;
    }

    public boolean isUseFlash() {
        return useFlash;
    }

    public void setUseFlash(boolean useFlash) {
        this.useFlash = useFlash;
    }

    public PersonProvident getPer() {
        return per;
    }

    public void setPer(PersonProvident per) {
        this.per = per;
    }

    public HouseProvident getHou() {
        return hou;
    }

    public void setHou(HouseProvident hou) {
        this.hou = hou;
    }

    public Person getP() {
        return p;
    }

    public void setP(Person p) {
        this.p = p;
    }

    public List<HouseProvident> getHouList() {
        return houList;
    }

    public void setHouList(List<HouseProvident> houList) {
        this.houList = houList;
    }

    public Integer getIyear() {
        return iyear;
    }

    public void setIyear(Integer iyear) {
        this.iyear = iyear;
    }

    public List<Integer> getYearList() {
        return yearList;
    }

    public void setYearList(List<Integer> yearList) {
        this.yearList = yearList;
    }
}
